package com.example.english.controller;

import com.example.english.pojo.Result;
import com.example.english.pojo.Teacher;
import com.example.english.service.LoginService;
import com.example.english.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;


    /**
     *    登录校验
     */
    @PostMapping("/login")
    public Result login(@RequestBody Teacher teacher){
        log.info("管理员登录；{}",teacher);

        Teacher a =  loginService.login(teacher);

        //登录成功，生成令牌，下发令牌
        if(a != null){

            Map<String,Object> claims = new HashMap<>();
            claims.put("Account",a.getAccount());
            claims.put("Password",a.getPassword());

            String jwt = JwtUtils.generateJwt(claims);

            return Result.success(jwt);
        }

        //登录失败，返回错误信息
        return Result.error("用户名或者密码错误!");
    }
}
